Accounts Merge

Try to solve the Accounts Merge problem.

Statement#

You are given a 2D array, accounts, where each row, accounts[i], is an array of strings, such that the first element, accounts[i][0], is a name, while the remaining elements are emails associated with that account. Your task is to determine if two accounts belong to the same person by checking if both accounts have the same name and at least one common email address.

If two accounts have the same name, they might belong to different people since people can have the same name. However, all accounts that belong to one person will have the same name. This implies that a single person can hold multiple accounts.

The output should be a 2D array in which the first element of each row is the name, and the rest of the elements are the merged list of that user’s email addresses in sorted order. There should be one row for each distinct user, and for each user, each email address should be listed only once.

Note: Please use a sort function that sorts the email addresses based on the ASCII value of each character.

Constraints:

  • 1≤1 \leq accounts.length ≤1000\leq 1000

  • 2≤2 \leq accounts[i].length ≤10\leq 10

  • 1≤1 \leq accounts[i][j].length ≤30\leq 30

  • Because accounts[i][0] is the name of any person, it should contain only English letters.

  • For j>0j>0, accounts[i][j] should be a valid email.

Examples#

Created with Fabric.js 3.6.6 Accounts Alice a9@mail.com a3@mail.com Harry h7@mail.com h1@mail.com Harry h1@mail.com h8@mail.com Alice a3@mail.com a5@mail.com Input Output Alice a3@mail.com a5@mail.com a9@mail.com Harry h1@mail.com h7@mail.com h8@mail.com Sample Example 1 ----------------------------------------------------------------------------------------------- Merged accounts with sorted emails The first and the fourth accounts are merged because they share the email "a3@mail.com".Similarly, the second and third accounts share the email "h1@mail.com", so they got merged.

1 of 2

Created with Fabric.js 3.6.6 Accounts Input Output Sarah s@mail.com sh@mail.com David ds@mail.com dd@mail.com John j8@mail.com j0@mail.com Sarah s@mail.com sh@mail.com David dd@mail.com ds@mail.com John j0@mail.com j8@mail.com Sample Example 2 ----------------------------------------------------------------------------------------------- None of the accounts share an email, so they are not merged. Each account belongs to a different individual. Accounts with sorted emails

2 of 2

Understand the problem#

Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

3

Consider the following accounts:

Accounts = [ [Sarah, s1@mail.com, s12@mail.com, s123@mail.com],

             [Sarah, s54@mail.com, s45@mail.com],

             [Sarah, s@mail.com, sh@mail.com, s7@mail.com] ]

What is the number of accounts in the output?

Your Answer
A)

1

B)

2

Correct Answer
C)

3

Explanation

None of the accounts share any email addresses, so the final output will have three accounts.

D)

4

Question 3 of 33 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Assign unique IDs to each account.

Iterate over each account. For each account, go over each email, and set their IDs as per the accounts to which they belong.

Merge all accounts that share the same email(s) into a single account.

Iterate over the list of merged accounts, and sort the emails in each account.


Try it yourself#

Implement your solution in main.py in the following coding playground. You will need the provided supporting code to implement your solution.

Python
main.py
union_find.py
Powered by AI
Input #1
Accounts Merge

Solution: Regions Cut by Slashes

Solution: Accounts Merge